package com.apps.gomdiem.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.apps.gomdiem.R;
import com.apps.gomdiem.common.DialogUtils;
import com.apps.gomdiem.common.DialogUtils.OnConfirmListener;
import com.apps.gomdiem.helper.DataBaseHelper;
import com.apps.gomdiem.model.UserAccount;
import com.apps.gomdiem.view.InputRequirement;

public class RegisterActivity extends Activity {
	private InputRequirement required;
	private EditText name, phone, email, pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_acitivy);
		initLayout();
	}

	private void initLayout() {
		name = (EditText) findViewById(R.id.name);
		phone = (EditText) findViewById(R.id.phonenum);
		email = (EditText) findViewById(R.id.email);
		pwd = (EditText) findViewById(R.id.password);
		EditText retypePwd = (EditText) findViewById(R.id.retype_pwd);
		required = new InputRequirement();
		required.setBlankErrorMsg(getString(R.string.error_blank));
		required.setMailErrorMsg(getString(R.string.error_email));
		required.addBlank(pwd)
				.addMail(email)
				.addAtLeast(getString(R.string.error_atleast), phone, email)
				.addMissMatch(getString(R.string.error_missmatch), pwd, retypePwd);
	}

	public void onRegister(View v) {
		if (!required.acceptRequirements())
			return;
		UserAccount userAccount = UserAccount.getInstance(RegisterActivity.this);
		userAccount.mPassword = pwd.getText() + "";
		userAccount.mName = name.getText() + "";
		userAccount.mEmail = email.getText() + "";
		userAccount.mPhone = phone.getText() + "";
		userAccount.mUserName = userAccount.mEmail + "_" + userAccount.mPhone;

		// Save temporary user account to local
		DataBaseHelper db = new DataBaseHelper(getApplicationContext());
		db.getWritableDatabase();
		db.addUserAccount(userAccount);
		Toast.makeText(getApplicationContext(), "Register success", Toast.LENGTH_LONG).show();
		finish();
	}

	public void onCancel(View v) {
		DialogUtils.makeConfirm(this, "Are you sure?", new OnConfirmListener() {
			@Override
			public void onOk() {
				finish();
			}
			
			@Override
			public void onCancel() {
			}
		}).show();
	}
}